package org.codert.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.stereotype.Component;

/**
 * @author: TanJingyu
 * @create: 2022-03-10 09:34
 **/
@Component
@Aspect
public class TestAspect {

//    @Around("execution(public * org.codert.service.*.*())")
//    public Object roundJointCut(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        MethodInvocationProceedingJoinPoint joinPoint = (MethodInvocationProceedingJoinPoint)proceedingJoinPoint;
//        System.out.println("前置");
//        Object proceed = joinPoint.proceed();

//        System.out.println("后置");
//
//        return proceed;
//    }

//    @Before("within(org.codert.service.DemoService) && args(address)")
//    public void beforeJointCut(String address) throws Throwable {
//        System.out.println("啊");
//    }


    @Around("target(org.codert.dao.A)")
    public Object aroundA(ProceedingJoinPoint pjp) {
        DataThreadLocalUtil.setResources("a");
        try {
            return pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            DataThreadLocalUtil.setResources("default");
        }

        return null;
    }

    @Around("target(org.codert.dao.B)")
    public Object aroundB(ProceedingJoinPoint pjp) {
        DataThreadLocalUtil.setResources("b");
        try {
            return pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            DataThreadLocalUtil.setResources("default");
        }

        return null;
    }

}

    